﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Darius.API.Application.Commands.Order
{
    public class ProductShowHandler : IRequestHandler<ProductShowCommand, ResultT<Product>>
    {
        private readonly IBaseRepositorys<Product> productRepository;
        private readonly ILogger<ProductShowHandler> logger;

        public ProductShowHandler(IBaseRepositorys<Product> productRepository, ILogger<ProductShowHandler> logger)
        {
            this.productRepository = productRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 处理选中的产品
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultT<Product>> Handle(ProductShowCommand request, CancellationToken cancellationToken)
        {

            //var result = new ResultT<Product>() { Code = (int)CodeEnum.search };

            //string[] strings = request.ProductIds.Split(',');
            //foreach (var item in strings)
            //{
            //    var list = productRepository.GetAll().Where(p => p.ProductId == Convert.ToInt32(item)).ToList();
            //    if (list.Count > 0)
            //    {
            //        result.Data.AddRange(list);
            //    }
            //}
           
            //return result;

            var result = new ResultT<Product>() { Code = (int)CodeEnum.search };

            string[] strings = request.ProductIds.Split(',');
            List<Product> products = new List<Product>();
            foreach (var item in strings)
            {
                var list1 = productRepository.GetAll().FirstOrDefault(p => p.ProductId == Convert.ToInt32(item));
                products.Add(list1);
            }
            result.Data = products;


            return result;

            //var result = new Result() { Code = (int)CodeEnum.search };
            //var res = productRepository.GetAll().Where(x => request.ProductIds.Contains(x.ProductId)).ToList();
            //result.Data = res;
            //return result;
        }
    }
}
